import sqlite3 as sql
import os
import storm.locals as sls

def create_testdb(sql_fname):
    cxn = sql.connect(sql_fname)
    cursor = cxn.cursor()
    cursor.execute("""CREATE TABLE morphologies(
                         name VARCHAR(128) PRIMARY KEY,
                         age INTEGER,
                         branch_points INTEGER,
                         length REAL,
                         surface_area REAL
                    )"""
    )
    cxn.close()

class Cell(object):
    __storm_table__ = "morphologies"
    name = sls.Unicode(primary=True)
    age = sls.Int()
    branch_points = sls.Int()
    length = sls.Float()
    surface_area = sls.Float()


db_target = "/tmp/test.sqlite"
os.remove(db_target)
create_testdb(db_target)

db = sls.create_database("sqlite:" + db_target)
store = sls.Store(db)

new_cell = Cell()
new_cell.name = u"CA1_pyr_A123"
new_cell.age = 3
new_cell.branch_points = 5
new_cell.length = 15
new_cell.surface_area = 19.2

store.add(new_cell)
store.commit()

mytest = store.find(Cell, Cell.name == u"CA1_pyr_A123").one()
print mytest.age


